package com.weilele.mvvm.base


import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.weilele.mvvm.MvvmConf
import com.weilele.mvvm.base.helper.*
import com.weilele.mvvm.base.helper.annotation.dealViewModelAnnotation
import com.weilele.mvvm.utils.`object`.ScreenAdaptationObj
import com.weilele.mvvm.utils.result_contract.NavigateForResultHelper
import com.weilele.mvvm.utils.result_contract.PermissionForResultHelper

/**
 * 描述：mvvm--基类activity
 */
abstract class MvvmActivity : AppCompatActivity(), BaseMvvmInterface {
    val navigateForResultHelper by lazy { NavigateForResultHelper(this) }
    val permissionForResultHelper by lazy { PermissionForResultHelper(this) }

    /**
     * 是否需要屏幕适配
     */
    open fun isNeedScreenAdaptation() = true

    /**
     * 获取view
     */
    override fun getLayoutResOrView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): Any? {
        return getLayoutResOrView(inflater, container)
    }

    /**
     * 保存所有的注册监听者
     */
    private val observerLiveDatas by lazy { mutableListOf<LiveDataWrap>() }

    /**
     * onCreate
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //跳转帮助，必须在onCreate注册
        navigateForResultHelper
        permissionForResultHelper
        dealViewModelAnnotation()
        getObserverLiveData()?.also {
            it.observer(this)
            observerLiveDatas.addAll(it)
        }
        getObserverLiveDataForever()?.also {
            it.observeForever()
            observerLiveDatas.addAll(it)
        }
        when (val contentView = getLayoutResOrView(
                layoutInflater,
                findViewById(android.R.id.content),
                savedInstanceState
        )) {
            is Int -> {
                setContentView(contentView)
            }
            is View -> {
                setContentView(contentView)
            }
            null -> {
                //什么都不做
            }
            else -> {
                throw IllegalArgumentException("getLayoutResOrView() only return view/LayoutRes")
            }
        }
        initUi(savedInstanceState)
        getClickView()?.forEach {
            it?.setOnClickListener(this)
        }
        initData()
    }

    override fun setRequestedOrientation(requestedOrientation: Int) {
        //处理安卓8.0报错
        //Only fullscreen activities can request orientation
        ignoreError { super.setRequestedOrientation(requestedOrientation) }
    }


    /**
     * 屏幕适配
     */
    override fun getResources(): Resources {
        val resources = super.getResources()
        if (MvvmConf.IS_CAN_SCREEN_ADAPTATION && isNeedScreenAdaptation()) {
            ScreenAdaptationObj.setCustomDensity(resources)
        }
        return resources
    }

    override fun onDestroy() {
        observerLiveDatas.removeObserver()
        super.onDestroy()
    }
}